/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import backend.Item;
import backend.Tag;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import memcache.MyCache;
import org.apache.thrift.TException;

/**
 * interface between middle ware and front end
 *
 * @author chanhlt
 */
public class FrontendHandler implements frontend.MiddlewareFrontend.Iface {

    BackendHandler handler = new BackendHandler();

    public void Caching() throws TException{
        List<Tag> listTag=getAllTag();
        for(int i=0; i<listTag.size(); i++){
            getAllItemsIDhaveTag(listTag.get(i).tagID);
        }
    }
    
 
    /**
     * get all tag
     *
     * @return
     * @throws TException
     */
    @Override
    public List<Tag> getAllTag() throws TException {

        List<Tag> listTag = null;
        try {
            listTag = (List<Tag>) MyCache.getInstance().get("listTag");
            if (listTag == null) {
                listTag = handler.getAllTag();                
                Properties pro = new Properties();
                pro.load(new FileInputStream("src/conf/config.ini"));
                int ttl = Integer.parseInt(pro.getProperty("ttl"));                
                MyCache.getInstance().set("listTag", ttl, listTag);           
                //System.out.println("get from database");
            }
//            } else {
//                System.out.println("get from memcache");
//            }
        } catch (IOException ex) {
            Logger.getLogger(FrontendHandler.class.getName()).log(Level.SEVERE, null, ex);
        }


        return listTag;
    }

    @Override
    public boolean insertTag(String tagName) throws TException {
        return handler.insertTag(tagName);
    }

    @Override
    public boolean deleteTag(String tagID) throws TException {
        return handler.deleteTag(tagID);
    }

    @Override
    public boolean deleteAllTag(List<String> tagIDs) throws TException {
        return handler.deleteAllTag(tagIDs);
    }

    @Override
    public boolean editTag(String tagID, String tagName) throws TException {
        return handler.editTag(tagID, tagName);
    }

    @Override
    public Tag getTag(String tagID) throws TException {
        return handler.getTag(tagID);
    }

    @Override
    public void setViewCountTag(String tagID) throws TException {

        handler.setViewCountTag(tagID);

    }

    @Override
    public List<Tag> getTopTags(long number) throws TException {
        return getTopTags(number);
    }

    /**
     * get number random items by taking one random item in number times
     *
     * @param number
     * @return
     * @throws TException
     */
    @Override
    public List<Item> getAllItems(long number) throws TException {

        List<Item> listItem = new ArrayList<>();
        //call getRandomItem number times

        for (int i = 0; i < number; i++) {
            Item item = getRandomItem();
            listItem.add(item);
        }

        return listItem;
    }

    @Override
    public List<Item> getAllItemshaveTag(String tagID, int numberItems) throws TException {
        return handler.getAllItemshaveTag(tagID, numberItems);
    }

    @Override
    public List<Item> pagingItemsTag(String tagID, int pageNumber, int numberItems) throws TException {
        return handler.pagingItemsTag(tagID, pageNumber, numberItems);
    }

    @Override
    public List<String> getAllItemsIDhaveTag(String tagID) throws TException {
        List<String> listItemID = null;
        String key=tagID+"listItemID";
        try {
            listItemID=(List<String>) MyCache.getInstance().get( key);
            if(listItemID==null){
                listItemID=handler.getAllItemsIDhaveTag(tagID);
                MyCache.getInstance().set(key, 3600 , listItemID);
            }
        } catch (IOException ex) {
            Logger.getLogger(FrontendHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listItemID;
    }

    /**
     * get a random item
     *
     * @return
     * @throws TException
     */
    @Override
    public Item getRandomItem() throws TException {
        Tag tag=getRandomTag();
        Item item=getRandomItemhaveTag(tag.tagID);
        return item;
    }
    public int getRandomIndex(int size){        
        int index=(new Random()).nextInt(size);
        return index;
    }
    
    public Tag getRandomTag() throws TException{
        List<Tag> listag=getAllTag();
        int index=getRandomIndex(listag.size());
        Tag tag=listag.get(index);
        return tag;
    }
    
    @Override
    public Item getRandomItemhaveTag(String tagID) throws TException {
        Item item=null;
        //an com roi lam tiep
        List<String> listitem=getAllItemsIDhaveTag(tagID);
        int index=getRandomIndex(listitem.size());
        String itemID=listitem.get(index);
        item=getItemFromItemID(itemID);
        return item;
    }

    @Override
    public void increaseViewCountItem(String itemID) throws TException {
        handler.increaseViewCountItem(itemID);
    }

    @Override
    public boolean deleteItem(String itemID) throws TException {
        return handler.deleteItem(itemID);
    }

    @Override
    public boolean deleteAllItem(List<String> itemIDs) throws TException {
        return handler.deleteAllItem(itemIDs);
    }

    @Override
    public boolean editItem(String itemID, String newItemValue, List<String> newTagIDs) throws TException {
        return handler.editItem(itemID, newItemValue, newTagIDs);
    }

    @Override
    public List<Item> getItemKeyword(String keyWord) throws TException {
        return handler.getItemKeyword(keyWord);
    }

    @Override
    public List<Item> getItemKeywordTag(String keyWord, String tagID) throws TException {
        return handler.getItemKeywordTag(keyWord, tagID);
    }

    @Override
    public List<Item> getTopItems(long number) throws TException {
        return handler.getTopItems(number);
    }

    @Override
    public List<Item> getTopItemsofTag(long number, String tagID) throws TException {
        return handler.getTopItemsofTag(number, tagID);
    }

    @Override
    public boolean insertFavoriteItem(List<Item> listFavoriteItems, Item item) throws TException {
        return handler.insertFavoriteItem(listFavoriteItems, item);
    }

    @Override
    public boolean deleteFavoriteItem(List<Item> listFavoriteItems, Item item) throws TException {
        return handler.deleteFavoriteItem(listFavoriteItems, item);
    }

    @Override
    public boolean blockUser(String userName) throws TException {
        return blockUser(userName);
    }

    @Override
    public boolean unblockUser(String userName) throws TException {
        return unblockUser(userName);
    }

    @Override
    public boolean deleteUser(String usrName) throws TException {
        return handler.deleteUser(usrName);
    }

    @Override
    public Item getItemFromItemID(String itemID) throws TException {
        Item item =null;
        try {
            item =(Item) MyCache.getInstance().get(itemID);
            if(item==null){
                item=handler.getItemFromItemID(itemID);
                MyCache.getInstance().set(itemID, 3600, item);
            }
        } catch (IOException ex) {
            Logger.getLogger(FrontendHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return item;
    }

    @Override
    public List<Item> getItemsFromListItemID(List<String> itemIDs) throws TException {
        return handler.getItemsFromListItemID(itemIDs);
    }

    @Override
    public void increaseLikeCountItem(String itemID) throws TException {
        handler.increaseLikeCountItem(itemID);
    }

    @Override
    public void increaseDislikeCountItem(String itemID) throws TException {
        handler.increaseDislikeCountItem(itemID);
    }

    @Override
    public String insertItem(String content, List<String> tagIDs) throws TException {
        return handler.insertItem(content, tagIDs);
    }

    @Override
    public List<Item> getItemsPage(long pageNumber, long itemNumber, String tagID) throws TException {
        return handler.getItemsPage(pageNumber, itemNumber, tagID);
    }

    @Override
    public List<Item> getFavoriteItems() throws TException {
        return handler.getFavoriteItems();
    }

    @Override
    public boolean addUser(String userID, String userToken, int userRole) throws TException {
        return handler.addUser(userID, userToken, userRole);
    }
}
